//全局变量
window.shop_id = 0

function workSatationModelShow(e){
//	console.log("进入维修厂工位管理页面");
	//获取维修厂id
	shop_id=$(e).data("sid")
//	console.log("维修厂id"+ shop_id)
	//查询维修厂信息
	getData(shop_id,showForm)
    //根据维修厂的工位信息决定是否添加
	model_edit(submit,cancel)
}

//ajax从后台获取数据展示
function getData(shop_id,successCall){
	//根据shopId获取到当前的工位情况
	$.ajax({
        url: projectContext + 'searchWorkStationById',
        type: 'GET',
        data: {
            'repairId': parseInt(shop_id)
        },
        success: successCall
    });
}


//向页面展示数据
function showForm(){
	getData(shop_id,function(data){
			if(data.data != ''){
				$(data.data).each(function(index,element){
				weekday = element.weekday;
				var idInput = "#weekdayinput"+weekday;
				var idTimeStart = "#timestart"+weekday;
				var idTimeEnd = "#timeend"+weekday;
				var idWorkStationId = "#WorkShopId"+weekday;
//				console.log("星期"+element.weekday);
//				console.log("工位数"+Number(element.stationCount));
//				console.log("上班时间"+element.openTime.slice(0, 2)+":00");
//				console.log("下班时间"+element.closeTime.slice(0, 2)+":00"+"\n");
//				console.log("工位id"+ element.id);
				$(idInput).val(Number(element.stationCount));
				$(idTimeEnd).val(element.closeTime.slice(0, 2)+":00");
				$(idTimeStart).val(element.openTime.slice(0,2)+":00");
				$(idWorkStationId).val(element.id);
				})
			}else{
				console.log("jinle ")
				for(var index = 1 ;index <=7 ;index++){
					var idInput = "#weekdayinput"+index;
					var idTimeStart = "#timestart"+index;
					var idTimeEnd = "#timeend"+index;
					var idWorkStationId = "#WorkShopId"+index;
					$(idInput).val(0);
					$(idTimeEnd).val("00:00");
					$(idTimeStart).val("00:00");
					$(idWorkStationId).val(0);
				}
				
			}
	})
}

//模态框中的见添加和取消按钮
function model_edit(okFun,cancelFun){

	$('#edit-workStation-modal').modal({
        wrapClassName: '.customer-class',
        width: '600',
        footer: 1,
        okText: "保存",
        cancelText: '取消',
        onOk: okFun,
        onCancel: cancelFun
    })
    
    var flag = 1;
    //监听复选框
      $("#edit_mode").change(function(){
    	  if(flag == 1){
			  console.log("去disabled");
			  $("input[name='workStationInput']").attr("disabled",false);
			  $("select[name='time_select']").attr("disabled",false);
			  flag = 0; 
		  }else{
			  console.log("添加disabled");
			  $("input[name='workStationInput']").attr("disabled",true);
			  $("select[name='time_select']").attr("disabled",true);
			  flag = 1;
		  }
      })
}

//定义工作日列表
var weekdays=[
	{
		carRepairShopId:0,
		closeTime:"",
		id:0,
		openTime:"",
		stationCount:0,
		weekday:1
	},
	{
		carRepairShopId:0,
		closeTime:"",
		id:0,
		openTime:"",
		stationCount:0,
		weekday:2
	},
	{
		carRepairShopId:0,
		closeTime:"",
		id:0,
		openTime:"",
		stationCount:0,
		weekday:3
	},
	{
		carRepairShopId:0,
		closeTime:"",
		id:0,
		openTime:"",
		stationCount:0,
		weekday:4
	},
	{
		carRepairShopId:0,
		closeTime:"",
		id:0,
		openTime:"",
		stationCount:0,
		weekday:5
	},
	{
		carRepairShopId:0,
		closeTime:"",
		id:0,
		openTime:"",
		stationCount:0,
		weekday:6
	},
	{
		carRepairShopId:0,
		closeTime:"",
		id:0,
		openTime:"",
		stationCount:0,
		weekday:7
	}
]




//修改维修厂信息
function submit(){
	//判断是否处在可编辑模式
	if( $("#edit_mode").is(":checked")){
		console.log("处在编辑模式无法提交");
		alert("处在编辑模式,无法执行提交操作");
		return false;
	}else{
		console.log("进入提交函数")
		for (var index =1 ;index <=7;index ++){
			var idInput = "#weekdayinput"+index;
			var idTimeStart = "#timestart"+index;
			var idTimeEnd = "#timeend"+index;
			var idWorkStationId = "#WorkShopId"+index;
			weekdays[index-1].weekday = index;
			weekdays[index-1].carRepairShopId = shop_id;
			weekdays[index-1].closeTime=$(idTimeEnd).find("option:selected").val();
			weekdays[index-1].openTime=$(idTimeStart).find("option:selected").val();
			weekdays[index-1].stationCount=Number($(idInput).val());
			weekdays[index-1].id=Number($(idWorkStationId).val());
		}
		for(var index = 0; index < 7;index++){
			console.log("星期几"+ weekdays[index].weekday+"维修厂id"+weekdays[index].carRepairShopId+"工位关门时间"+
					weekdays[index].closeTime+"工位开门时间"+weekdays[index].openTime+"工位数"+weekdays[index].stationCount+"\n");
		}
		//修改完成提交
		$.ajax({
		       url: projectContext + 'updateworkStation',
		       data: JSON.stringify(weekdays),
		       contentType:'application/json;charset=utf-8',
//			   dataType:'json',
		       type: 'PUT',
		       success: function (data) {
//		    	   console.log(data);
		    	   if(data.code === 200){
		    		   alert("操作成功");
//		    		   console.log("执行成功");
		    	   }else{
		    		   alert("操作失败");
//		    		   console.log("执行失败");
		    	   }
		    	   
		       },
		       error: function () {
		    	   alert("执行失败");
//		    	   console.log("执行失败")
		       }
		    })
	}
}





function workStation(shop_id){
	//获取一周7天的工位数
	for(var index = 1; index <=7;index++){
		weekdays[index].carRepairShopId = shop_id;
	}
}

//取消修改
function cancel(){
	console.log("取消修改")
}



